{% extends "cmdb/mequipment/index.html" %}

{% block right_content %}
<div id="right_menu" xmlns="http://www.w3.org/1999/html">
    <b class="modal-title">机房编辑</b>
    <hr/>
    <form class="form-horizontal">
        <div class="form-inline" style="padding-left:27px">
            <label for="id">编号</label>
            <input class="form-control" style="width:400px;" type="text" id="id" value="{{ server_room_info.id }}" disabled/>
        </div><br />
        <div class="form-inline" style="padding-left:13px">
            <label for="serverRoom">机房名</label>
            <input class="form-control" style="width:400px;" type="text" id="serverRoom" value="{{ server_room_info.server_room }}" />
            <span style="color:red">*必填</span>
        </div><br />
        <div class="form-inline" style="padding-left:13px">
            <label>房间号</label>
            <input class="form-control" style="width:400px;" type="text" id="roomNo_0" value="{{ server_room_info.first_room_no }}" />
            <img src="{{ STATIC_URL }}img/add.png" onclick="addRoomNo()">
            <div style="padding-left:45px">
                <table id="roomNoInfo">
                {% if server_room_info.room_no_list %}
                    {% for idx,single_room in server_room_info.room_no_list.items %}
                    <tr>
                        <td>
                            <input class="form-control" style="width:400px;" type="text" id="roomNo_{{ idx }}" value="{{ single_room }}" />
                        </td>
                        <td>
                            <img src="/static/img/delete.png" onclick="delRoomNo(this)" />
                        </td>
                    </tr>
                    {% endfor %}
                {% endif %}
                </table>
            </div>
        </div><br />
        <div class="form-inline">
            <label for="phone" style="vertical-align: top;">机房电话</label>
            <input class="form-control" style="width:400px;" type="text" id="phone" value="{{ server_room_info.phone }}" />
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline" >
            <label for="address" style="vertical-align: top;">机房地址</label>
            <input class="form-control" style="width:400px;" type="text" id="address" value="{{ server_room_info.server_room_addr }}" />
            <input type="text" style="display:none" id="temp_id">
        </div><br />
        <div class="form-inline" style="padding-left:27px">
            <label for="comment">备注</label>
            <input class="form-control" style="width:400px;" type="text" id="comment" value="{{ server_room_info.comment }}" />
            <span style="display:none;">*必填</span>
        </div><br />
        <input type="text" id="operator" value="{{ request.session.username }}" style="display: none;">
    </form>
    <div style="padding-left: 100px;">
        <button type="button" class="btn btn-primary" id="item_edit">
            确定
        </button>&nbsp;&nbsp;&nbsp;
        <button type="button" class="btn btn-default" id="edit_cancel">
            取消
        </button>
    </div>
</div>

<script type="text/javascript">
$("#item_edit").click(function(){
    var id = $("#id").val();
    var serverRoom = $("#serverRoom").val();
    if(!serverRoom || serverRoom == ''){
        alert('机房名不能为空');
        return;
    }
    var roomNoArr = new Array();
    var roomNo = $("#roomNo_0").val();
    if(roomNo != ""){
        roomNoArr.push(roomNo);
    }
    for(var i=0; i < $("#roomNoInfo").children().find('input').length; i++){
        roomNo = $("#roomNoInfo").children().find('input').eq(i).val();
        if(roomNo!=''){
            roomNoArr.push(roomNo);
        }
    }
    var phone = $("#phone").val();
    var address = $("#address").val();
    var comment = $("#comment").val();
    var operator = $("#operator").val();

    var urls = "{% url 'equipment:seditaction' %}";
    var postdata = {id:id,server_room:serverRoom,room_no_info_list:JSON.stringify(roomNoArr), phone:phone, server_room_addr:address, comment:comment, operator:operator}
    var jsonstr = $.ajax({
			url : urls,
			type : "POST",
			data : postdata,
			async : false,
			cache : false
		});
		var jsonobj = eval('(' + jsonstr.responseText + ')');
		if(jsonobj.status==0){
		    alert(jsonobj.message);
		}else{
            window.location.href="{% url 'equipment:roominfo' %}?page=" + {{ page }};
        }
});

$("#edit_cancel").click(function(){
    window.location.href="{% url 'equipment:roominfo' %}?page=" + {{ page }};
});
var count = {{ room_count }}
function addRoomNo(){
    var trId = 'tr_' + count;
    var roomId = 'roomNo_' + count;
    $("#roomNoInfo").append('<tr><td><input class="form-control" style="width:400px;" type="text" id="'+roomId+'" placeholder="房间号" /></td><td><img src="/static/img/delete.png" onclick="delRoomNo(this)" /></td></tr>');
    count++;
}
function delRoomNo(obj){
    var row = obj.parentNode.parentNode;
    row.parentNode.removeChild(row);
}
</script>
{% endblock %}